package com.app.milkteaapp.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.app.milkteaapp.bean.RegisterGsonBean;
import com.app.milkteaapp.R;
import com.app.milkteaapp.utils.GsonUtils;
import com.app.milkteaapp.utils.MyTools;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_username;
    private EditText et_password;
    private EditText et_phone;
    private Button btn_register;
    private Spinner sp_gender;
    private EditText et_password_1;
    private String username;
    private String password;
    private String phone;
    private String password_1;
    Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    private void initView() {
        et_username = (EditText) findViewById(R.id.et_username);
        et_password = (EditText) findViewById(R.id.et_password);

        et_phone = (EditText) findViewById(R.id.et_phone);
        btn_register = (Button) findViewById(R.id.btn_register);

        btn_register.setOnClickListener(this);
        sp_gender = (Spinner) findViewById(R.id.sp_gender);
        et_password_1 = (EditText) findViewById(R.id.et_password_1);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_register:
                if (submit()) {
                    if(password.equals(password_1)){
                        //json数据封装
                        JSONObject jsonObject = new JSONObject();
                        try {
                            jsonObject.put("name",username);
                            jsonObject.put("password",password);
                            jsonObject.put("sex",ifsex(sp_gender.getSelectedItem().toString()));
                            jsonObject.put("phone",phone);


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        OkHttpClient client = new OkHttpClient();

                        MediaType type = MediaType.parse("application/json;charset=utf-8");
                        RequestBody requestBody =RequestBody.create(type,jsonObject.toString());

                        Request request = new Request.Builder()
                                .post(requestBody)
                                .url(MyTools.URL+"/user/register")
                                .build();
                        Call call = client.newCall(request);

                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(context, "网络炸了", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }

                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                //gson解析
                                String string = response.body().string();
                                RegisterGsonBean registerGsonBean = GsonUtils.fromJson(string, RegisterGsonBean.class);
                                //如果请求成功,则返回登录页面
                                if (registerGsonBean.isSuccess()) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(context, "注册成功", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                    finish();
                                }
                            }
                        });

                    }else{
                        Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }
    }

    private boolean submit() {
        username = et_username.getText().toString().trim();
        password = et_password.getText().toString().trim();
        phone = et_phone.getText().toString().trim();
        password_1 = et_password_1.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            Toast.makeText(this, "username不能为空", Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "password不能为空", Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "phone不能为空", Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(password_1)) {
            Toast.makeText(this, "不能为空", Toast.LENGTH_SHORT).show();
        } else {
            return true;
        }
        return false;
    }

    private int ifsex(String sex){
        int i = 0;
        if(sex.equals("男")){
            i=1;
        }else if(sex.equals("女")){
            i=2;
        }
        return i;
    }
}
